package com.mobi.spring1.controller;

import com.mobi.spring1.Sever.impl.LoginSever;
import com.mobi.spring1.pojo.Login;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "登入控制类")
@Controller
@RequestMapping("/login")
class LoginController {

    @GetMapping("/hello")
    @ResponseBody
    public Map<String, Object> showHelloWorld(){
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "HelloWorld");
        return map;
    }


    @Autowired
    private LoginSever ls;


    @GetMapping("/add")
    @ResponseBody
    public void add(@RequestBody Login login){
        ls.add(login);

    }

    @GetMapping("/del")
    @ResponseBody
    public void del(@RequestBody Integer id){
        ls.del(id);
    }

    @GetMapping("/upd")
    @ResponseBody
    public void upd(@RequestBody Login login){
        ls.add(login);
    }

    @GetMapping("/findById")
    @ResponseBody
    public Map<String, Object> findById(@RequestBody Integer id){
        Map<String, Object> map = new HashMap<>();
        Object byId = ls.findById(id);
        map.put("findById",byId);
        return map;
    }

    @GetMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(){
        Map<String, Object> map = new HashMap<>();
        System.out.println(ls);
        map.put("findById",ls.findAll());
        return map;
    }

    //登入接口
    @GetMapping("/login")
    @ResponseBody
    public Map<String, Object> login(@Param("username") String username, @Param("password") String password, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        boolean byUsername = ls.findByUsername(username, password);
        if (byUsername){
            HttpSession session = request.getSession();
            session.setAttribute("loginuser",ls.findByUsername(username));
        }else {
            HttpSession session = request.getSession();
            session.removeAttribute("loginuser");
        }
        map.put("flag",byUsername);
        return map;
    }

    //查询是否是老师
    @GetMapping("/findisteacher")
    @ResponseBody
    public Map<String, Object> findisteacher(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        HttpSession session = request.getSession();
        Login loginuser = (Login)session.getAttribute("loginuser");
        if (loginuser==null||"".equals(loginuser.getIsTeacher())){
            map.put("flag",false);
            return map;
        }
        if ("1".equals(loginuser.getIsTeacher())){
            map.put("flag",true);
        }else {
            map.put("flag",false);
        }

        return map;
    }
}
